home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / internet-tools / ipdial / source / deviceio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-02  |  4.3 KB  |  253 lines

  1. /**
  2. ***  IPDial - DeviceIO.c
  3. ***
  4. ***  This file implements something like a device class. It's only
  5. ***  reason is the lack of a possibility to determine the real state
  6. ***  of an IORequest. (Sent? Inactive? Whatever else?)
  7. ***  This file implements dynamically growing buffers.
  8. ***
  9. ***
  10. ***  This program is free software; you can redistribute it and/or modify
  11. ***  it under the terms of the GNU General Public License as published by
  12. ***  the Free Software Foundation; either version 2 of the License, or
  13. ***  (at your option) any later version.
  14. ***
  15. ***  This program is distributed in the hope that it will be useful,
  16. ***  but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ***  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18. ***  GNU General Public License for more details.
  19. ***
  20. ***  You should have received a copy of the GNU General Public License
  21. ***  along with this program; if not, write to the Free Software
  22. ***  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. ***
  24. ***  Authors: Jochen Wiedmann <wiedmann@neckar-alb.de>
  25. ***           Stefan Gybas <cab@studbox.uni-stuttgart.de>
  26. **/
  27.  
  28.  
  29.  
  30.  
  31.  
  32. /**
  33. ***  Include files
  34. **/
  35. #ifndef IPDIAL_H
  36. #include "IPDial.h"
  37. #endif
  38. #include <exec/devices.h>
  39. #include <exec/io.h>
  40.  
  41.  
  42.  
  43.  
  44.  
  45. /**
  46. ***  We use the structure below for device IO, because I don't see
  47. ***  any way to check an IORequest whether it must be aborted, before
  48. ***  using or deleting it.
  49. **/
  50. struct MyIORequest
  51. { ULONG Used;
  52.   ULONG Signal;
  53.   ULONG DeviceOpen;
  54.   struct IORequest *Req;
  55. };
  56.  
  57.  
  58.  
  59.  
  60.  
  61. /**
  62. ***  This is the WaitIO() equivalent.
  63. **/
  64. BYTE DeviceIOWait(APTR wreq)
  65.  
  66. { struct MyIORequest *req = wreq;
  67.   BYTE result;
  68.  
  69.   result = WaitIO(req->Req);
  70.   req->Used = FALSE;
  71.   return(result);
  72. }
  73.  
  74.  
  75.  
  76.  
  77.  
  78. /**
  79. ***  This is the AbortIO() equivalent.
  80. **/
  81. VOID DeviceIOAbort(APTR areq)
  82.  
  83. { struct MyIORequest *req = areq;
  84.  
  85.   if (req  &&  req->Used  &&  req->Req)
  86.   { AbortIO(req->Req);
  87.     DeviceIOWait(req);
  88.   }
  89.   /**
  90.   ***  The following is needed for reasons I don't understand
  91.   ***  really.
  92.   **/
  93.   SetSignal(0, req->Signal);
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100. /**
  101. ***  This is the SendIO() equivalent.
  102. **/
  103. VOID DeviceIOSend(APTR sreq, UWORD Command)
  104.  
  105. { struct MyIORequest *req = sreq;
  106.  
  107.   if (req->Used)
  108.   { PrintError(0, "IORequest in use");
  109.   }
  110.   req->Used = TRUE;
  111.   req->Req->io_Command = Command;
  112.   SendIO(req->Req);
  113. }
  114.  
  115.  
  116.  
  117.  
  118.  
  119. /**
  120. ***  This is the DoIO() equivalent.
  121. **/
  122. BYTE DeviceIODo(APTR dreq, UWORD Command)
  123.  
  124. { struct MyIORequest *req = dreq;
  125.  
  126. #if DELAY_AFTER_IO > 0
  127.   BYTE   status;
  128. #endif
  129.  
  130.   if (req->Used)
  131.   { PrintError(0, "IORequest in use");
  132.   }
  133.   req->Req->io_Command = Command;
  134.  
  135. #if DELAY_AFTER_IO > 0
  136.  
  137.   status = DoIO(req->Req);
  138.   Delay(DELAY_AFTER_IO);
  139.   return(status);
  140.  
  141. #else
  142.  
  143.   return(DoIO(req->Req));
  144.  
  145. #endif
  146.  
  147. }
  148.  
  149.  
  150.  
  151.  
  152.  
  153. /**
  154. ***  This is the DeleteIORequest() equivalent.
  155. **/
  156. VOID DeviceIODelete(APTR dreq)
  157.  
  158. { struct MyIORequest *req = dreq;
  159.  
  160.   if (req)
  161.   { DeviceIOAbort(req);
  162.     if (req->DeviceOpen)
  163.     { CloseDevice(req->Req);
  164.     }
  165.     if (req->Req)
  166.     { struct MsgPort *port = req->Req->io_Message.mn_ReplyPort;
  167.  
  168.       DeleteIORequest(req->Req);
  169.       DeleteMsgPort(port);
  170.     }
  171.     free(req);
  172.   }
  173. }
  174.  
  175.  
  176.  
  177.  
  178.  
  179. /**
  180. ***  This is the CreateIORequest() equivalent.
  181. **/
  182. APTR DeviceIOCreate(ULONG Size)
  183.  
  184. { struct MyIORequest *req;
  185.  
  186.   if ((req = malloc(sizeof(*req))))
  187.   { struct MsgPort *port;
  188.  
  189.     req->Used = FALSE;
  190.     req->DeviceOpen = FALSE;
  191.     if ((port = CreateMsgPort()))
  192.     { req->Signal = (1 << port->mp_SigBit);
  193.       if ((req->Req = CreateIORequest(port, Size)))
  194.       { return(req);
  195.       }
  196.       DeleteMsgPort(port);
  197.     }
  198.     free(req);
  199.   }
  200.   return(NULL);
  201. }
  202.  
  203.  
  204.  
  205.  
  206.  
  207. /**
  208. ***  This is the OpenDevice() equivalent.
  209. **/
  210. BYTE DeviceIOOpen(STRPTR devname,
  211.                   ULONG unit,
  212.                   APTR oreq,
  213.                   ULONG flags)
  214.  
  215. { struct MyIORequest *req = oreq;
  216.   BYTE error;
  217.  
  218.   if (req->DeviceOpen)
  219.   { PrintError(0, "device already open");
  220.   }
  221.  
  222.   if (!(error = OpenDevice(devname, unit, req->Req, flags)))
  223.   { req->DeviceOpen = TRUE;
  224.   }
  225.   return(error);
  226. }
  227.  
  228.  
  229.  
  230.  
  231.  
  232. /**
  233. ***  This function returns the IORequest attached to a struct
  234. ***  MyIORequest.
  235. **/
  236. struct IORequest *DeviceIOReq(APTR rreq)
  237.  
  238. { return(((struct MyIORequest *)rreq)->Req);
  239. }
  240.  
  241.  
  242.  
  243.  
  244.  
  245. /**
  246. ***  This function returns an IORequest's signal. (Not the
  247. ***  signal number.)
  248. **/
  249. ULONG DeviceIOSignal(APTR sreq)
  250.  
  251. { return(((struct MyIORequest *) sreq)->Signal);
  252. }
  253.